import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
r = requests.get('https://www.lagou.com/', headers = headers)
content = r.text
# 把刚刚保存在content中的文件放入Beautiful Soup中
soup = BeautifulSoup(content, 'lxml')
smalls = soup.find_all(class_ = 'menu_sub dn')
bigs = soup.find_all(class_ = 'category-list')
i = 0

result="["
for big in bigs:
    result=result+"  {"
    title = big.h2.get_text().strip()
    result=result+'"name": "'+title+'",'+'"sub": ['
    spans=smalls[i].find_all('span')
    dds=smalls[i].find_all('dd')
    j=0
    for span in spans:
        result=result+"  {"
        title1=span.get_text().strip()
        result=result+'"name": "'+title1+'",'+'"sub": ['
        lasts=dds[j].find_all('a')
        for last in lasts:
            result=result+"  {"
            title2=last.get_text().strip()
            result=result+'"name": "'+title2+'"'
            result=result+"  },"
        result=result[:-1]
        result=result+"  ]"
        result=result+"  },"
        j=j+1
    i = i+1
    result=result[:-1]
    result=result+"  ]"
    result=result+"  },"
result=result[:-1]
result=result+"  ]"
f=open('a.txt','w',encoding='utf-8')
f.write(result)
f.close()